package com.fl.nj.controller;

import com.fl.nj.entity.ServiceType;
import com.fl.nj.service.ServiceTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 服务类型Controller
 * 提供RESTful API接口，用于管理服务类型
 */
@RestController
@RequestMapping("/api/service-types")
public class ServiceTypeController {
    
    @Autowired
    private ServiceTypeService serviceTypeService;
    
    /**
     * 获取服务类型列表
     * @return 服务类型列表
     */
    @GetMapping
    public ResponseEntity<List<ServiceType>> getAllServiceTypes() {
        List<ServiceType> serviceTypes = serviceTypeService.getAll();
        return ResponseEntity.ok(serviceTypes);
    }
    
    /**
     * 分页查询服务类型
     * @param page 页码
     * @param pageSize 每页条数
     * @param typeName 服务类型名称关键词（可选）
     * @return 服务类型列表及分页信息
     */
    @GetMapping("/page")
    public ResponseEntity<Map<String, Object>> getServiceTypesByPage(
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer pageSize,
            @RequestParam(required = false) String typeName) {
        
        Map<String, Object> result = new HashMap<>();
        try {
            Map<String, Object> params = new HashMap<>();
            params.put("page", page);
            params.put("pageSize", pageSize);
            params.put("typeName", typeName);
            
            List<ServiceType> serviceTypes = serviceTypeService.getPageList(params);
            int total = serviceTypeService.getTotal(params);
            
            result.put("success", true);
            result.put("data", serviceTypes);
            result.put("total", total);
            result.put("page", page);
            result.put("pageSize", pageSize);
            return ResponseEntity.ok(result);
            
        } catch (Exception e) {
            result.put("success", false);
            result.put("message", "获取服务类型列表失败: " + e.getMessage());
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(result);
        }
    }
    
    /**
     * 根据ID获取服务类型
     * @param id 服务类型ID
     * @return 服务类型信息
     */
    @GetMapping("/{id}")
    public ResponseEntity<Map<String, Object>> getServiceTypeById(@PathVariable Long id) {
        Map<String, Object> result = new HashMap<>();
        try {
            ServiceType serviceType = serviceTypeService.getById(id);
            if (serviceType == null) {
                result.put("success", false);
                result.put("message", "服务类型不存在");
                return ResponseEntity.status(HttpStatus.NOT_FOUND).body(result);
            }
            result.put("success", true);
            result.put("data", serviceType);
            return ResponseEntity.ok(result);
            
        } catch (Exception e) {
            result.put("success", false);
            result.put("message", "获取服务类型失败: " + e.getMessage());
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(result);
        }
    }
    
    /**
     * 创建服务类型
     * @param serviceType 服务类型信息
     * @return 创建结果
     */
    @PostMapping
    public ResponseEntity<Map<String, Object>> createServiceType(@RequestBody ServiceType serviceType) {
        Map<String, Object> result = new HashMap<>();
        try {
            // 验证参数
            if (serviceType.getTypeName() == null || serviceType.getTypeName().trim().isEmpty()) {
                result.put("success", false);
                result.put("message", "服务类型名称不能为空");
                return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(result);
            }
            
            boolean success = serviceTypeService.save(serviceType);
            if (success) {
                result.put("success", true);
                result.put("message", "服务类型创建成功");
                result.put("data", serviceType);
                return ResponseEntity.status(HttpStatus.CREATED).body(result);
            } else {
                result.put("success", false);
                result.put("message", "服务类型名称已存在");
                return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(result);
            }
            
        } catch (Exception e) {
            result.put("success", false);
            result.put("message", "创建服务类型失败: " + e.getMessage());
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(result);
        }
    }
    
    /**
     * 更新服务类型
     * @param id 服务类型ID
     * @param serviceType 服务类型信息
     * @return 更新结果
     */
    @PutMapping("/{id}")
    public ResponseEntity<Map<String, Object>> updateServiceType(@PathVariable Long id, @RequestBody ServiceType serviceType) {
        Map<String, Object> result = new HashMap<>();
        try {
            // 验证参数
            if (serviceType.getTypeName() == null || serviceType.getTypeName().trim().isEmpty()) {
                result.put("success", false);
                result.put("message", "服务类型名称不能为空");
                return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(result);
            }
            
            serviceType.setId(id);
            boolean success = serviceTypeService.update(serviceType);
            if (success) {
                result.put("success", true);
                result.put("message", "服务类型更新成功");
                result.put("data", serviceType);
                return ResponseEntity.ok(result);
            } else {
                // 检查是因为不存在还是名称重复
                ServiceType existing = serviceTypeService.getById(id);
                if (existing == null) {
                    result.put("success", false);
                    result.put("message", "服务类型不存在");
                    return ResponseEntity.status(HttpStatus.NOT_FOUND).body(result);
                } else {
                    result.put("success", false);
                    result.put("message", "服务类型名称已存在");
                    return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(result);
                }
            }
            
        } catch (Exception e) {
            result.put("success", false);
            result.put("message", "更新服务类型失败: " + e.getMessage());
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(result);
        }
    }
    
    /**
     * 删除服务类型
     * @param id 服务类型ID
     * @return 删除结果
     */
    @DeleteMapping("/{id}")
    public ResponseEntity<Map<String, Object>> deleteServiceType(@PathVariable Long id) {
        Map<String, Object> result = new HashMap<>();
        try {
            boolean success = serviceTypeService.delete(id);
            if (success) {
                result.put("success", true);
                result.put("message", "服务类型删除成功");
                return ResponseEntity.ok(result);
            } else {
                result.put("success", false);
                result.put("message", "服务类型不存在");
                return ResponseEntity.status(HttpStatus.NOT_FOUND).body(result);
            }
            
        } catch (Exception e) {
            result.put("success", false);
            result.put("message", "删除服务类型失败: " + e.getMessage());
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(result);
        }
    }
}